(function ($) {
	var sourceData;
	var perInfo,pageType,nurseid,perid
	var domArr = [{'domId':'HireDuty','tip':'','type':'list','key':'HireDuty'},
				{'domId':'HireStDate','tip':'','type':'date','key':'HireStDate'},
				{'domId':'HireEndDate','tip':'','type':'date','key':'HireEndDate'},
				{'domId':'HireRemark','tip':'','type':'input','key':'HireRemark'},
				{'domId':'RowID','key':'RowID'},
				{'domId':'MajorDuty','tip':'请选择专业技术职称','type':'list','key':'MajorDuty'},
				{'domId':'MajorDutyDate','tip':'专业技术职称取得时间','type':'date','key':'MajorDutyDate'}]
	var unUploadFiles=[] //未上传的图片
	$.init();
	$.plusReady(function() {
		baseSetup();
	})
	if (!mui.os.plus) {
		baseSetup()
	}
	//HireDuty|12__3^HireStDate|2022-08-01^HireEndDate|2022-08-31^HireRemark|备注^RowID|35^MajorDuty|12__3^MajorDutyDate|2022-07-31^HirePerDR|1
	function submitNewData(){
		blurAllInput()
		var parr = ''
		for (var i = 0; i < domArr.length; i++) {
			var oneDom = domArr[i]
			if (oneDom['domId'] == 'RowID') {
				if (sourceData) {
					parr += 'RowID|'+sourceData['hireRowID']+'^'
				}else{
					parr += 'RowID|^'
				}
				continue
			}
			var dom = $('#'+oneDom['domId'])[0]
			var domValue = dom.getAttribute('data-id')
			domValue = (domValue == undefined?"":domValue)
			if (oneDom['type'] == 'input') {
				domValue = dom.value
			}else if (oneDom['type'] != 'list') {
				domValue = dom.innerText
			}
			if (domValue == '' && oneDom['tip']!= '') {
				mui.toast(oneDom['tip'])
				return
			}
			parr += oneDom['key']+'|'+domValue+'^'
		}
		parr += 'HirePerDR|'+perid
		var status = this.getAttribute('data-id');

		var pdict = {'parr':parr,'type':status}
		var paramDict = {'method':'web.INMPersonComm:SaveDuty','params':JSON.stringify(pdict),'type':'Method'}
		$.DHCWebService(paramDict, function(result) {
			//console.log(JSON.stringify(result))
			var msgcode =result['msgcode']
			if (msgcode == '-1') {
				mui.toast(result['msg'])
				return 
			}
			
			if (unUploadFiles.length==0 ) {
				plus.webview.getWebviewById('../WKPlatformPages/apply/applyList.html').evalJS('detailChanged()')
				mui.back()
				return;
			}
			var path = 'hire'
			var method = 'web.INMPersonComm:InsertPic'
			var paramDict={'id':perid,'path':path,'imgArr':unUploadFiles,'method':method,'otherParams':result['msg']+'||0||','spStr':'^'}
			saveFileAction(paramDict,function(flag){
				if (!flag) {
					return
				}
				unUploadFiles=[]
				plus.webview.getWebviewById('../WKPlatformPages/apply/applyList.html').evalJS('detailChanged()')
				mui.back()
			})
		}, function(errorStr) {
			mui.toast(errorStr)
		});
	}
	
	function getTransDetail(){
		var pdict = {'id':sourceData['hireRowID']};
		var method = 'web.INMPersonComm:GetDutyItem'
		var paramDict = {'method':method,'params':JSON.stringify(pdict),'type':'RecMethod'}
		$.DHCWebService(paramDict, function(result) {
			//console.log(JSON.stringify(result))
			var dataDict = result.data
			updateUI(dataDict)
		}, function(errorStr) {
		 	
		});
	}
	
	
	function updateUI(dataDict){
		for (var i = 0; i < domArr.length; i++) {
			var oneDom = domArr[i]
			if (oneDom['domId'] == 'RowID') {
				continue
			}
			var dom = $('#'+oneDom['domId'])[0]
			if (oneDom['type'] == 'input') {
				dom.value=dataDict[oneDom['key']]
			}else if (oneDom['type'] == 'list'){
				var value = dataDict[oneDom['key']+'Desc']
				dom.innerText=value
				dom.setAttribute('data-id',dataDict[oneDom['key']])
			}else{
				dom.innerText=dataDict[oneDom['key']]
			}
		}
		updateImageFileUI('hirFile','fileUL',dataDict,dataDict['RowID'])
	}

	function baseSetup(){
		$('.mui-scroll-wrapper').scroll({
			bounce: true,
			indicators: false, //是否显示滚动条
		});
		mui.previewImage();
		var self = plus.webview.currentWebview()
		sourceData = self.data
		pageType = self.pageType
		perInfo = self.perInfo
		nurseid = perInfo['loginID']
		perid = perInfo['loginPerID']
		if (sourceData) { //修改
			getTransDetail()
		}
		$('#CreatorName')[0].innerText = perInfo['loginName']
		$('#applyLoc')[0].innerText = perInfo['loginWardDesc']

		//关闭按钮
		mui('body').on('tap','#closeBtn',function(){
			blurAllInput()
			mui.back()
		})
		//保存提交按钮
		mui('body').on('tap','.submitBtn',submitNewData)
		// 日期
		mui('#UL').on('tap','.dateType',selectDate);
		mui('#UL').on('tap','.listType',downListElementTapped);
		window.addEventListener('resize',windowResize)
		$('#addFileBtn').on('change','input',function(){
			var imgArr = this.files
			var name = imgArr[0]['name']
			if (fileTypeIcon(name) != 'img') {
				mui.toast('仅支持图片哦~~')
				return;
			}
			unUploadFiles = afterSelectImageFile('fileUL',unUploadFiles,imgArr)
		});
		//监听文件删除按钮
		mui('#fileUL').on('tap','.mui-icon-closeempty',function(){
			var method = 'web.INMPersonComm:DeleteCert'
			var otherParams = '||0||'
			var paramDict={'imgArr':unUploadFiles,'method':method,'nurseid':nurseid,'otherParams':otherParams}
			deleteFileBtnTapped(this,paramDict,function(flag,arr){
				if (arr) {
					unUploadFiles = arr
				}
			});
		}) 
		mui.back = function(){
			if (unUploadFiles.length > 0) {
				mui.confirm('您有未上传的文件','温馨提示',['放弃','继续操作'],function (e) {
					if (e.index == 0) {
						plus.nativeUI.closeWaiting()
						plus.webview.currentWebview().close()
					}
				},'div')
				return
			}
			plus.nativeUI.closeWaiting()
			plus.webview.currentWebview().close()
		}
	}
	
})(mui)

